package cn.wolfcode.controller;

import cn.wolfcode.cache.util.ParamMap;
import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.Travel;
import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.search.domain.DestinationEs;
import cn.wolfcode.search.domain.StrategyEs;
import cn.wolfcode.search.domain.TravelEs;
import cn.wolfcode.search.domain.UserInfoEs;
import cn.wolfcode.search.query.SearchQueryObject;
import cn.wolfcode.search.service.ISearchService;
import cn.wolfcode.search.service.IStrategyEsService;
import cn.wolfcode.search.vo.SearchResultVO;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private ISearchService searchService;


    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        String kw = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(kw);
        switch (qo.getType()){
            case  SearchQueryObject.TYPE_DEST :
                //目的地
                return searchDest(qo);
            case  SearchQueryObject.TYPE_STRATEGY :
                //攻略
                return searchStrategy(qo);
            case  SearchQueryObject.TYPE_TRAVEL :
                //游记
                return searchTravel(qo);
            case  SearchQueryObject.TYPE_USER :
                //用户
                return searchUser(qo);
            default:
                //全部
                return searchAll(qo);
        }
    }

    private Object searchAll(SearchQueryObject qo) {
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                "name", "info");


        SearchResultVO vo = new SearchResultVO();

        vo.setDests(ds.getContent());
        vo.setStrategys(sts.getContent());
        vo.setTravels(ts.getContent());
        vo.setUsers(us.getContent());

        vo.setTotal(sts.getTotalElements() + ts.getNumberOfElements() + us.getTotalElements() + ds.getTotalElements());

        return JsonResult.success(new ParamMap().put("result", vo).put("qo", qo));
    }

    private Object searchUser(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");
        return JsonResult.success(new ParamMap().put("page", page).put("qo", qo));
    }

    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");

        return JsonResult.success(new ParamMap().put("page", page).put("qo", qo));
    }

    private Object searchStrategy(SearchQueryObject qo) {
        //攻略
            Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                    "title", "subTitle", "summary");
            return JsonResult.success(new ParamMap().put("page", page).put("qo", qo));
    }

    private Object searchDest(SearchQueryObject qo) {
        //1:通过关键字查询目的地是否存在
        Destination dest = destinationService.getByName(qo.getKeyword());
        SearchResultVO data = new SearchResultVO();
        if(dest != null){
            //2:存在: 查询指定目的地下的攻略, 游记, 用户 约定前5篇
            List<Strategy> sts = strategyService.getByDestName(qo.getKeyword());
            data.setTotal(sts.size() + data.getTotal());
            if(sts.size() > 5){
                sts = sts.subList(0,5);
            }
            List<Travel> ts = travelService.getByDestName(qo.getKeyword());
            data.setTotal(ts.size() + data.getTotal());
            if(ts.size() > 5){
                ts = ts.subList(0,5);
            }
            List<UserInfo> us = userInfoService.getByCity(qo.getKeyword());
            data.setTotal(us.size() + data.getTotal());
            if(us.size() > 5){
                us = us.subList(0,5);
            }
            data.setUsers(us);
            data.setStrategys(sts);
            data.setTravels(ts);
        }
        //3:不存在: 提示不存在
        return JsonResult.success(new ParamMap().put("result", data).put("dest", dest).put("qo", qo));
    }
}

